package io.bdmc.common.utils;

import io.bdmc.core.udp.UdpClientSendData;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Random;

@Component
public class SMSUtils {
    @Autowired
    UdpClientSendData udpClient;

    protected static Logger logger = LoggerFactory.getLogger(SMSUtils.class);

    /**
     * 测试短信网关
     * 
     * @param ip   ip
     * @param port 端口
     * @return 是否成功
     */
    public boolean testSMSGateway(String ip, Integer port) {
        String parseData = udpClient.sendTcpData(ip, port, ByteUtil.str2HexStr("at") + "0D");
        String result = ByteUtil.HexToStr(parseData);
        boolean b = result.contains("OK");
        return b;
    }

    /**
     * 发送长短信
     * 
     * @param ip      ip
     * @param port    端口
     * @param mobile  手机号
     * @param message 消息
     * @return
     */
    public boolean sendSMSLong(String ip, Integer port, String mobile, String message) {
        List<String> sendMsgLt = sendLongMessageOrder(mobile, message);
        boolean b = false;
        for (String sendMsg : sendMsgLt) {
            String send = ByteUtil.str2HexStr("at+cmgs=" + (sendMsg.length() / 2 - 1)) + "0D";
            String parseData = udpClient.sendSMSData(ip, port, send);
            parseData = udpClient.sendSMSData(ip, port, ByteUtil.str2HexStr(sendMsg));
            parseData = udpClient.sendSMSData(ip, port, "1a");
            String result = ByteUtil.HexToStr(parseData);
            b = result.contains("OK");
        }

        return b;
    }

    /**
     * 测试短信网关
     * 
     * @param ip   ip
     * @param port 端口
     * @return 是否成功
     */
    public boolean sendSMS(String ip, Integer port, String mobile, String message) {
        String sendMsg = sendMessageOrder(mobile, message);
        String send = ByteUtil.str2HexStr("at+cmgs=" + (sendMsg.length() / 2 - 1)) + "0D";
        String parseData = udpClient.sendSMSData(ip, port, send);
        parseData = udpClient.sendSMSData(ip, port, ByteUtil.str2HexStr(sendMsg));
        parseData = udpClient.sendSMSData(ip, port, "1a");
        String result = ByteUtil.HexToStr(parseData);
        boolean b = result.contains("OK");
        return b;
    }

    private String sendMessageOrder(String phoneNum, String message) {
        String allMsg = "0011000b81";
        if (phoneNum.length() == 11) {
            phoneNum += "f";
        }

        char[] chars = phoneNum.toCharArray();
        for (int i = 0; i < 6; i++) {
            allMsg += chars[2 * i + 1];
            allMsg += chars[2 * i];
        }
        allMsg += "0008a7";

        String hex = ByteUtil.bytesToHex(UnicodeConvertUtil.putString2UnicodeBytes(message, true));
        allMsg += ByteUtil.intToHex_1(hex.length() / 2);
        allMsg += hex;
        return allMsg;
    }

    private List<String> sendLongMessageOrder(String phoneNum, String message) {
        List<String> lt = new ArrayList<String>();
        String hex = ByteUtil.bytesToHex(UnicodeConvertUtil.putString2UnicodeBytes(message, true));
        int allLen = hex.length();
        int count = allLen / 152 + 1;
        Random r = new Random(1);
        int ran1 = r.nextInt(65000);
        for (int i = 0; i < count; i++) {
            String data = "";
            if (i == count - 1) {
                data = hex.substring(152 * i);
            } else {
                data = hex.substring(152 * i, 152 * i + 152);
            }

            String allMsg = "0051000b81";
            if (phoneNum.length() == 11) {
                phoneNum += "f";
            }
            char[] chars = phoneNum.toCharArray();
            for (int j = 0; j < 6; j++) {
                allMsg += chars[2 * j + 1];
                allMsg += chars[2 * j];
            }
            allMsg += "0008a7";
            allMsg += ByteUtil.intToHex_1(data.length() / 2 + 6);

            allMsg += "050003" + ByteUtil.intToHex_1(ran1) + ByteUtil.intToHex_1(count) + ByteUtil.intToHex_1(i + 1);
            allMsg += data;
            lt.add(allMsg);
        }

        return lt;
    }
}
